package com.geek.novel.repository

import com.geek.novel.common.Constant
import com.geek.novel.entity.SysUser
import com.geek.novel.utils.GsonUtil
import com.geek.novel.view.loading.LoadingDialogUtil
import com.geek.novel.vo.Pager
import com.google.gson.JsonObject
import io.reactivex.Observable
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.ResponseBody
import java.io.File
import java.util.*

/**
 * Created by  on 2022/05/29
 */
class HttpRepository(private val apiService: WebRepository) {

    /**
     * 登录接口
     */
    fun login(username: String, password: String): Observable<JsonObject> {
        //显示loading
        LoadingDialogUtil.getInstance().showLoading("加载中...")
        //入参
        val map = HashMap<String, String>()
        map["username"] = username
        map["password"] = password
        return apiService.login(map)
    }

    /**
     * 获取用户信息接口
     */
    fun getUserInfo(): Observable<JsonObject> {
        return apiService.getUserInfo()
    }

    /**
     * 更新用户信息接口
     */
    fun updateUserInfo(user: SysUser): Observable<JsonObject> {
        //显示loading
        LoadingDialogUtil.getInstance().showLoading("数据保存中...")
        return apiService.updateUserInfo(user)
    }

    /**
     * 注册接口
     */
    fun regist(user: SysUser): Observable<JsonObject> {
        //显示loading
        LoadingDialogUtil.getInstance().showLoading("信息注册中...")
        //入参
        val map = JsonObject()
        map.add("data", GsonUtil.toJsonTree(user))
        return apiService.regist(map)
    }

    /**
     * 修改密码接口
     */
    fun modifyPassword(user: SysUser, password: String): Observable<JsonObject> {
        //显示loading
        LoadingDialogUtil.getInstance().showLoading("正在修改密码...")
        //入参
        val map = JsonObject()
        map.add("data", GsonUtil.toJsonTree(user))
        map.addProperty("password", password)
        return apiService.modifyPassword(map)
    }

    /**
     * 发送忘记密码的验证码
     */
    fun sendForgetPassValidateCode(target: String): Observable<JsonObject> {
        //显示loading
        LoadingDialogUtil.getInstance().showLoading("正在发送验证码...")
        //入参
        val map = JsonObject()
        map.addProperty("target", target)
        return apiService.sendForgetPassValidateCode(map)
    }

    /**
     * 忘记密码-修改密码
     */
    fun forgetPassword(target: String, type: String, code: String, password: String, passwordAgain: String): Observable<JsonObject> {
        //显示loading
        LoadingDialogUtil.getInstance().showLoading("正在修改密码...")
        //入参
        val map = JsonObject()
        map.addProperty("target", target)
        map.addProperty("type", type)
        map.addProperty("code", code)
        map.addProperty("password", password)
        map.addProperty("passwordAgain", passwordAgain)
        return apiService.forgetPassword(map)
    }

//    /**
//     * 更新Token
//     */
//    fun refreshPowerToken(): Observable<String> {
//        map.clear()
//        map.put("clientid", MyConstance.CLIENTID)
//        map.put("refreshkey", SPUtil[MyApp.instance, MyConstance.SP_refreshkey, "", SPUtil.usrInfo] as String)
//        return apiService.refreshPowerToken(map)
//    }

    /**
     * 上传图片
     */
    fun uploadFile(file: File, mediaType: String, fileType: String, fileId: Long?, fileName: String?=null, filePath: String?=null): Observable<JsonObject> {
        val requestBody = RequestBody.create(MediaType.parse(mediaType), file)
        val body = MultipartBody.Part.createFormData("file", file.name, requestBody)
        return apiService.uploadFile(body, fileType, fileId, fileName, filePath)
    }

    /**
     * 下载文件
     */
    fun downloadImage(id: Long): Observable<ResponseBody> {
        return apiService.downloadImage(id)
    }

    /**
     * 检查更新
     */
    fun checkUpdate(): Observable<JsonObject> {
        //入参
        val map = HashMap<String, String>()
        map["versionName"] = Constant.versionName
        map["versionCode"] = "${Constant.versionCode}"
        return apiService.checkUpdate(map)
    }

    /**
     * 获取字体列表
     */
    fun getTypefaceList(): Observable<JsonObject> {
        return apiService.getTypefaceList()
    }

    /**
     * 获取首页推荐小说
     */
    fun getRecommendBooks(): Observable<JsonObject> {
        return apiService.getRecommendBooks()
    }

    /**
     * 获取书籍详情
     */
    fun getBookDetail(bookId: String): Observable<JsonObject> {
        //入参
        val map = HashMap<String, String>()
        map["bookId"] = bookId
        return apiService.bookDetail(map)
    }

    /**
     * 根据作者id获取作者的所有书籍
     */
    fun getAuthorBooks(authorId: String, excludeBookId: String): Observable<JsonObject> {
        //入参
        val map = HashMap<String, String>()
        map["authorId"] = authorId
        map["excludeBookId"] = excludeBookId
        return apiService.getAuthorBooks(map)
    }

    /**
     * 根据作者id获取作者的所有书籍
     */
    fun bookSearch(keyword: String, category: Int?, pager: Pager<Any>): Observable<JsonObject> {
        //入参
        val map = JsonObject()
        map.addProperty("keyword", keyword)
        if(category != null){
            map.addProperty("category", category)
        }
        map.add("page", GsonUtil.toJsonTree(pager))
        return apiService.bookSearch(map)
    }

    /**
     * 获取所有爬虫源
     */
    fun getCrawlerSourceList(): Observable<JsonObject> {
        return apiService.getCrawlerSourceList()
    }

    /**
     * 获取小说分类
     */
    fun bookCategories(): Observable<JsonObject> {
        return apiService.bookCategories()
    }

    /**
     * 根据分类信息获取小说列表
     */
    fun queryBookByCategory(category: String, pager: Pager<Any>): Observable<JsonObject> {
        //入参
        val map = JsonObject()
        map.addProperty("category", category)
        map.add("page", GsonUtil.toJsonTree(pager))
        return apiService.queryBookByCategory(map)
    }

    /**
     * 根据bookId获取爬虫源信息
     */
    fun getBookCrawlerList(bookId: String): Observable<JsonObject> {
        //入参
        val map = JsonObject()
        map.addProperty("bookId", bookId)
        return apiService.getBookCrawlerList(map)
    }

    /**
     * 用户加收藏
     */
    fun addUserCollect(bookId: String): Observable<JsonObject> {
        //入参
        val map = JsonObject()
        map.addProperty("bookId", bookId)
        return apiService.addUserCollect(map)
    }

    /**
     * 用户删除收藏
     */
    fun removeUserCollect(bookId: String): Observable<JsonObject> {
        //入参
        val map = JsonObject()
        map.addProperty("bookId", bookId)
        return apiService.removeUserCollect(map)
    }


    /**
     * 获取用户收藏列表
     */
    fun queryUserCollected(pager: Pager<Any>): Observable<JsonObject> {
        //入参
        val map = JsonObject()
        map.add("page", GsonUtil.toJsonTree(pager))
        return apiService.queryUserCollected(map)
    }

    /**
     * 书籍点击数+1
     */
    fun addVisitCount(bookId: String): Observable<JsonObject> {
        //入参
        val map = JsonObject()
        map.addProperty("bookId", bookId)
        return apiService.addVisitCount(map)
    }

    /**
     * 书籍阅读数+1
     */
    fun addReadCount(bookId: String): Observable<JsonObject> {
        //入参
        val map = JsonObject()
        map.addProperty("bookId", bookId)
        return apiService.addReadCount(map)
    }

    /**
     * 书籍阅读数-1
     */
    fun minusReadCount(bookId: String): Observable<JsonObject> {
        //入参
        val map = JsonObject()
        map.addProperty("bookId", bookId)
        return apiService.minusReadCount(map)
    }

    /**
     * 热搜词
     */
    fun queryBookSearchHotKey(): Observable<JsonObject> {
        return apiService.queryBookSearchHotKey()
    }

}